package com.hongxun.design.activitys;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.adapters.SelectProjectAdapter;
import com.hongxun.design.bean.ProjectItemBean;
import com.hongxun.design.interfaces.HeadSecond;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.ui.dialogstyle.MyDialog;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.ui.ui_listview.refreshlist.SwipeRefreshListView;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.fileutil.GetFileUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/7/20.
 * 删除文件缓存
 */
public class SelectDelectProjectActivity extends BaseActivity implements HeadSecond, SwipeRefreshLayout.OnRefreshListener {
    SwipeRefreshListView listview;
    List<ProjectItemBean> data;
    SelectProjectAdapter adapter;
    String json;
    EditText serch_words;
    int type = 0;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    getData();
                    handler.sendEmptyMessage(2);
                    break;
                case 2:
                    if (adapter == null) {
                        adapter = new SelectProjectAdapter(mActivity, data);
                        listview.setAdapter(adapter);
                    } else {
                        listview.update();

                    }
                    listview.setRefreshing(false);
                    break;


            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selectproject);
        findBar();
        findView();
    }


    @Override
    public void findBar() {
        HeadSecondControlPanel headsecond_layout = (HeadSecondControlPanel) findViewById(R.id.selectproject_layout);
        headsecond_layout.initHeadPanel(DataUtil.getsecondbean("", View.GONE, R.color.bar_right, ListenerSet.back, "选择项目", R.color.title, View.VISIBLE, IconSourse.BACK + "", View.VISIBLE));
    }

    boolean flag = false;

    @Override
    protected void onResume() {
        if (flag) {
            Search(null);
        }
        flag = true;
        super.onResume();
    }

    @Override
    protected void findView() {
        super.findView();
        TextView serch_button = (TextView) findViewById(R.id.serch_button);
        serch_words = (EditText) findViewById(R.id.serch_words);
        SetTypeface.set(serch_button, IconSourse.SEARCH);
        serch_button.append(" 搜索");
        listview = (SwipeRefreshListView) findViewById(R.id.selectproject_listview);
        listview.setOnRefreshListener(this);
        listview.setDivider(6);
        getData();
        adapter = new SelectProjectAdapter(this, data);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               MyDialog.dialogFordelectProject((Activity)view.getContext(),data.get(i),handler);
            }
        });

    }

    @Override
    public void onRefresh() {
        String words = serch_words.getText().toString();
        if (words.length() == 0) {
            handler.sendEmptyMessage(1);
        } else {
            listview.setRefreshing(false);
        }
    }


    private void getData() {
        if (data == null) data = new ArrayList<>();
        data.clear();
        data.addAll(GetFileUtil.getProjectStageListCacheFile());
    }

    /**
     * 搜索*
     */
    public void Search(View v) {
        String words = serch_words.getText().toString();
        if (words.length() == 0) {
            handler.sendEmptyMessage(1);
        } else {
            List<ProjectItemBean> searchData = new ArrayList<>();
            for (ProjectItemBean p : data) {
                if (p.getProject_name().contains(words)) {
                    searchData.add(p);
                }
            }
            data.clear();
            data.addAll(searchData);
            handler.sendEmptyMessage(2);
        }
    }


}
